package com.example.ssm.service.impl;

import com.example.ssm.entity.SysUser;
import com.example.ssm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 自定义 UserDetailsService
 *
 * @author yaojianfeng
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        SysUser sysUser = userService.queryByName(name);
        //如果能查到 则将其封装成User对象, 包含其资源权限信息,否则返回空
        if (sysUser != null) {
            return User.withUsername(sysUser.getUserName()).password(sysUser.getPassword()).authorities("p2").build();
        }
        return null;
    }
}
